package cn.hl.sfzy.service.impl;

import cn.hl.sfzy.entity.*;
import cn.hl.sfzy.mapper.CourseMarkMapper;
import cn.hl.sfzy.mapper.CourseReplyMapper;
import cn.hl.sfzy.mapper.CourseTalkMapper;
import cn.hl.sfzy.mapper.UserMapper;
import cn.hl.sfzy.service.CourseTalkService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CourseTalkServiceImpl
 * @Description TODO
 * @Author libowen
 * @Date 2024年10月29日 10:19
 * @Version 1.0
 */
@Service
public class CourseTalkServiceImpl implements CourseTalkService {

    @Autowired
    private CourseTalkMapper courseTalkMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CourseReplyMapper courseReplyMapper;

    @Override
    public PageInfo<CourseTalk> findByCourseId(int pageNum, int pageSize, Long course_id) {
        PageHelper.startPage(pageNum, pageSize);
        List<CourseTalk> list = courseTalkMapper.selectByCourseId(course_id);
        for(CourseTalk ct : list){
            User user =
                    userMapper.selectById(ct.getUser_id());
            ct.setUser(user);
        }
        for(CourseTalk ct : list){
            List<CourseReply> reply =
                    courseReplyMapper.selectByTalkId(ct.getId());
            for (CourseReply cr:reply){
                User user =
                        userMapper.selectById(cr.getUser_id());
                cr.setUser(user);
            }
            ct.setReply(reply);
            ct.setReplytotal(reply.size());
        }
        PageInfo<CourseTalk> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public List<CourseTalk> findByCourseIdOrder1(Long course_id) {
        List<CourseTalk> list = courseTalkMapper.selectByCourseIdOrder1(course_id);
        for(CourseTalk ct : list){
            User user =
                    userMapper.selectById(ct.getUser_id());
            ct.setUser(user);
        }
        for(CourseTalk ct : list){
            List<CourseReply> reply =
                    courseReplyMapper.selectByTalkId(ct.getId());
            for (CourseReply cr:reply){
                User user =
                        userMapper.selectById(cr.getUser_id());
                cr.setUser(user);
            }
            ct.setReply(reply);
            ct.setReplytotal(reply.size());
        }
        return list;
    }

    @Override
    public List<CourseTalk> findByCourseIdOrder2(Long course_id) {
        List<CourseTalk> list = courseTalkMapper.selectByCourseIdOrder2(course_id);
        for(CourseTalk ct : list){
            User user =
                    userMapper.selectById(ct.getUser_id());
            ct.setUser(user);
        }
        for(CourseTalk ct : list){
            List<CourseReply> reply =
                    courseReplyMapper.selectByTalkId(ct.getId());
            for (CourseReply cr:reply){
                User user =
                        userMapper.selectById(cr.getUser_id());
                cr.setUser(user);
            }
            ct.setReply(reply);
            ct.setReplytotal(reply.size());
        }
        return list;
    }



}
